package com.app.micaihu.view.main.game;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;

import com.android.volley.VolleyError;
import com.app.micaihu.R;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.BaseListActivity;
import com.app.micaihu.base.bean.DataBean;
import com.app.micaihu.bean.game.GameRadidersList;
import com.app.micaihu.bean.game.GameRaidersEntity;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.configure.URIContant;
import com.app.micaihu.listener.HttpListener;
import com.app.micaihu.view.main.game.adapter.GameRaidersAdapter;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 游戏攻略列表页面
 * Created by linkai on 2017/3/12.
 */
public class GameRaidersListActivity extends BaseListActivity<GameRaidersEntity> {

    private String gameId;//游戏id

    @Override
    protected void changeView() {
        gameId = getIntent().getStringExtra(Config.Extra.PARAMETER1);
    }

    @Override
    protected void changeData(final boolean isRefresh) {

        HashMap<String, String> paramMap = new HashMap<>();
        paramMap.put("page", dataPage + "");
        paramMap.put("gameId", gameId);

        HttpListener<DataBean<GameRadidersList>> blackListListener = new HttpListener<DataBean<GameRadidersList>>() {

            @Override
            public void onStart() {
                super.onStart();
                setLoadView(LOADING, null);
            }

            @Override
            public void onError(VolleyError error) {
                setLoadView(NO_NET, AppApplication.getInstance().getString(R.string.neterror_click));
            }

            @Override
            public void onSuccess(DataBean<GameRadidersList> response) {
                if (response.noErrorData()) {
                    List<GameRaidersEntity> blacklists = response.getData().getStrategyList();
                    if (blacklists != null && !blacklists.isEmpty()) {
                        if (dataList == null) {
                            dataList = new ArrayList<>();
                        }
                        if (isRefresh) {
                            //清空数据
                            dataList.clear();
                        }
                        dataList.addAll(blacklists);
                        if (mAdapter == null) {
                            mAdapter = new GameRaidersAdapter(dataList, GameRaidersListActivity.this);
                            mListView.setAdapter(mAdapter);
                        } else {
                            mAdapter.notifyDataSetChanged();
                        }
                        setLoadView(LOADSUCES, null);
                    } else {
                        setLoadView(DATAEMPTY, TypeConstant.EMPTYVIEW_DEFAULT, getString(R.string.pullup_no_data), null, null);
                    }
                } else {
                    setLoadView(DATAERROR, response.getNnderstoodMsg());
                }
            }
        };


        addPostCacheRequest(URIContant.GAME_RAIDERS_LIST, new TypeToken<DataBean<GameRadidersList>>() {
        }.getType(), paramMap, blackListListener);
    }

    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        if (dataList == null) {
            return;
        }
        position--;
        //防止越界
        if (position >= dataList.size()) {
            position = dataList.size() - 1;
        } else if (position < 0) {
            position = 0;
        }
        GameRaidersEntity newsEntity = dataList.get(position);
        if (newsEntity == null) {
            return;
        }
        Intent intent = new Intent();
        intent.putExtra(Config.Extra.PARAMETER1, newsEntity.getArticleId());
        intent.putExtra(Config.Extra.PARAMETER2, newsEntity.getGameId());
        if (TextUtils.equals(newsEntity.getArticleType(), TypeConstant.GAME_RAIDERS_TEXT)) {
            intent.setClass(this, GameRaidersNewsActivity.class);
        } else if (TextUtils.equals(newsEntity.getArticleType(), TypeConstant.GAME_RAIDERS_VIDEO)) {
            intent.setClass(this, GameRaidersVideoActivity.class);
        }
        startActivity(intent);
    }
}
